package com.api.workflow.web;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.Cell;
import weaver.soa.workflow.request.DetailTable;
import weaver.soa.workflow.request.RequestInfo;
import weaver.soa.workflow.request.Row;

/**
 * @author wanghan
 * @version 1.0
 * @Description 应付账款冻结
 * @since 2025/2/27 12:29
 */
public class AccountsPayableFrozenAction extends BaseBean implements Action {

    @Override
    public String execute(RequestInfo requestInfo) {
        try {

            //获得流程id
            String workflowid = requestInfo.getWorkflowid();
            //获取表id
            String requestid = requestInfo.getRequestid();
            writeLog("AccountsPayableFrozenAction-----start----------------------" + requestid);
            RecordSet rs = new RecordSet();
            rs.execute("select tablename from workflow_base a,workflow_bill b where a.formid=b.id and a.id=" + workflowid);
            rs.next();
            //获取主表名
            String maintable = Util.null2String(rs.getString("tablename"));
            writeLog("maintablemaintablemaintable" + maintable);
            DetailTable[] detailTable = requestInfo.getDetailTableInfo().getDetailTable();
            writeLog("detailTabledetailTable" + detailTable);
            JSONArray arrayListmx1 = new JSONArray();
            DetailTable detailTablemx1 = detailTable[0];
            Row[] rowmx1 = detailTablemx1.getRow();
            if (rowmx1.length > 0) {
                for (Row r : rowmx1) {//遍历明细1
                    JSONObject jsonObject = new JSONObject();
                    Cell[] cell = r.getCell();
                    for (Cell c1 : cell) {
                        jsonObject.put(c1.getName(), c1.getValue());
                    }
                    arrayListmx1.add(jsonObject);

                }
            }
            writeLog("arrayListmx1" + arrayListmx1);
            if (arrayListmx1.size() > 0) {
                for (int i = 0; i < arrayListmx1.size(); i++) {
                    String sql = "";
                    JSONObject jsonObject = arrayListmx1.getJSONObject(i);
                    String sjid = jsonObject.getString("sjid");
                    writeLog("detailTabledetailTable===sjid:" + sjid);
                    RecordSet recordSet = new RecordSet();
                    String select = "select * from uf_yfzkbd where id= ?";
                    recordSet.executeQuery(select, sjid);
                    if (recordSet.next()) {
                        String gsdm1 = recordSet.getString("gsdm");
                        String hjnd1 = recordSet.getString("hjnd");
                        String hjpzdpzbh1 = recordSet.getString("hjpzdpzbh");
                        String hjpzzdxxmbh1 = recordSet.getString("hjpzzdxxmbh");
                        String wybs = gsdm1 + hjnd1 + hjpzdpzbh1 + hjpzzdxxmbh1;
                        sql = "select * from uf_yfzkbd where wybs=? and ycbs=1";
                        writeLog("detailTabledetailTable===wybs:" + wybs);
                        rs.executeQuery(sql, wybs);
                        if (rs.next()) {
                            String hjpzdpzbh = Util.null2String(rs.getString("hjpzdpzbh"));
                            String hjpzzdxxmbh = Util.null2String(rs.getString("hjpzzdxxmbh"));
                            String hjnd = Util.null2String(rs.getString("hjnd"));
                            requestInfo.getRequestManager().setMessagecontent("明细表中存在会计凭证的凭证编号为：" + hjpzdpzbh + " 会计凭证中的行项目编号为：" + hjpzzdxxmbh + " 会计年度为：" + hjnd + " 对应的应付账款已被使用不可再次选择！");
                            return Action.FAILURE_AND_CONTINUE;
                        }
                    }


                }
            }
            if (arrayListmx1.size() > 0) {
                for (int i = 0; i < arrayListmx1.size(); i++) {
                    String sql = "";
                    JSONObject jsonObject = arrayListmx1.getJSONObject(i);
                    String sjid = jsonObject.getString("sjid");
                    writeLog("detailTabledetailTable===sjid:" + sjid);
                    RecordSet recordSet = new RecordSet();
                    String select = "select * from uf_yfzkbd where id= ?";
                    writeLog("detailTabledetailTable===arrayListmx1:" + sjid);
                    recordSet.executeQuery(select, sjid);
                    if (recordSet.next()) {
                        String gsdm1 = recordSet.getString("gsdm");
                        String hjnd1 = recordSet.getString("hjnd");
                        String hjpzdpzbh1 = recordSet.getString("hjpzdpzbh");
                        String hjpzzdxxmbh1 = recordSet.getString("hjpzzdxxmbh");
                        String wybs = gsdm1 + hjnd1 + hjpzdpzbh1 + hjpzzdxxmbh1;
                        writeLog("detailTabledetailTable===arrayListmx1wybs:" + wybs);
                        sql = "update uf_yfzkbd set ycbs=1 where wybs=?";
                        rs.executeUpdate(sql, wybs);
                    }
                }
            }
            return Action.SUCCESS;

        } catch (Exception e) {
            e.printStackTrace();
            requestInfo.getRequestManager().setMessagecontent("接口出错,请联系管理员!" + e.getMessage());
            return Action.FAILURE_AND_CONTINUE;
        }
    }
}
